<template>
  <page-container>
    <pagination-table
      v-model:form-data="queryForm"
      border="inner"
      :tree-config="{ childrenField: 'children', expandAll: true }"
      :load="loadTableData"
    >
      <template #form="{ bindForm, submit, reset }">
        <el-form :ref="bindForm" inline :model="queryForm">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="queryForm.name" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="所属部门" prop="dept">
            <dept-select v-model="queryForm.dept" multiple> </dept-select>
          </el-form-item>
          <el-form-item style="margin-right: 0">
            <el-space>
              <el-button type="primary" @click="submit">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-space>
          </el-form-item>
        </el-form>
      </template>
      <template #actions>
        <el-button type="primary" @click="add">新增</el-button>
      </template>
      <tree-column title="部门名称" field="name" row-field="id"> </tree-column>
      <vxe-column title="操作" fixed="right" width="120">
        <template #default="scope">
          <el-space class="tool-color">
            <el-button link type="primary" @click.stop="update(scope.row)"
              >编辑</el-button
            >
            <el-popover placement="bottom-end">
              <template #reference>
                <el-button type="primary" link @click.stop>更多</el-button>
              </template>

              <el-space size="large" vertical>
                <el-button
                  block
                  link
                  type="primary"
                  @click="addChild(scope.row)"
                >
                  新增下级
                </el-button>
                <el-button link block type="danger"> 删除 </el-button>
              </el-space>
            </el-popover>
          </el-space>
        </template>
      </vxe-column>
    </pagination-table>
  </page-container>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useFloat from '@/hooks/useFloat';
  import { getDeptData } from '@/api/common';
  import EditRow from './rowForm.vue';

  const float = useFloat('dialog', EditRow);
  const add = () => {
    float.open({
      title: '新增',
      width: 600,
      props: {
        onConfirm() {
          // update data
          float.close();
        },
        onCancel() {
          float.close();
        }
      }
    });
  };

  const update = (row: any) => {
    float.open({
      title: '新增2',
      width: 600,
      props: {
        row,
        onConfirm() {
          // update data
          float.close();
        },
        onCancel() {
          float.close();
        }
      }
    });
  };

  const addChild = (parent: any) => {
    float.open({
      title: '新增',
      width: 600,
      props: {
        parent,
        onConfirm() {
          // update data
          float.close();
        },
        onCancel() {
          float.close();
        }
      }
    });
  };

  const queryForm = ref({
    name: '',
    dept: undefined
  });

  const loadTableData: loadPaginationRequest<typeof queryForm> = async ({
    queryParams
  }) => {
    console.log(queryParams);
    const dataList = await getDeptData();
    return Promise.resolve({ dataList });
  };
</script>
